package com.yunkaweilai.android.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.Umeng;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码第二次提交
 * Created by Administrator on 2017/7/22.
 */

public class ForgetPassStp3Activity extends BaseActivity {

    @BindView(R.id.id_edt_pass1)
    EditText idEdtPass1;
    @BindView(R.id.id_edt_pass2)
    EditText idEdtPass2;
    @BindView(R.id.id_check_pass)
    CheckBox idCheckPass;
    @BindView(R.id.id_check_pass2)
    CheckBox idCheckPass2;
    @BindView(R.id.iv_register_success)
    ImageView IvSuccess;
    @BindView(R.id.iv_register_success2)
    ImageView IvSuccess2;
    @BindView(R.id.tv_register_success)
    TextView TvSuccess;
    @BindView(R.id.tv_register_success2)
    TextView TvSuccess2;
    @BindView(R.id.ll_forget1)
    LinearLayout llForget1;
    @BindView(R.id.ll_forget2)
    LinearLayout llForget2;
    @BindView(R.id.id_btn_next)
    Button btnNext;

    private String forget_password_token;
    private boolean isRegister = false;
    public static final String FORGET_PASSWORD_TOKEN = "FORGET_PASSWORD_TOKEN";
    public static final String IS_REGISTER = "IS_REGISTER";

    // 忘记密码入口
    public static void actionStart(Context context, String forget_password_token) {
        Intent intent = new Intent(context, ForgetPassStp3Activity.class);
        intent.putExtra(FORGET_PASSWORD_TOKEN, forget_password_token);
        context.startActivity(intent);
    }

    // 注册入口
    public static void actionStart(Context context, boolean isRegister) {
        Intent intent = new Intent(context, ForgetPassStp3Activity.class);
        intent.putExtra(IS_REGISTER, isRegister);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forger_pass_stp3);
        ButterKnife.bind(this);
        forget_password_token = getIntent().getStringExtra(FORGET_PASSWORD_TOKEN);
        isRegister = getIntent().getBooleanExtra(IS_REGISTER,false);

        if (isRegister){
            // 友盟埋点
            MobclickAgent.onEvent(mAc, Umeng.ZHUCE);

            new TitleBuilder(mAc).setTitleText("注册成功")
                    .setLeftImage(R.mipmap.ic_go_back)
                    .setLeftOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
            llForget1.setVisibility(View.GONE);
            llForget2.setVisibility(View.GONE);
            IvSuccess.setVisibility(View.VISIBLE);
            IvSuccess2.setVisibility(View.VISIBLE);
            TvSuccess.setVisibility(View.VISIBLE);
            TvSuccess2.setVisibility(View.VISIBLE);
            btnNext.setText("返回登录");
        }
        else{
            new TitleBuilder(mAc).setTitleText("重置密码")
                    .setLeftImage(R.mipmap.ic_go_back)
                    .setLeftOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
            initClick();
        }
    }


    @OnClick({R.id.id_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_btn_next://下一步
                if (!isRegister){
                    isRight();
                }else {
                    intent2Activity(LoginActivity.class);
                }

                break;
        }
    }

    private void initClick() {
        idCheckPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中，显示密码
                    idEdtPass1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    idEdtPass1.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtPass1.setSelection(idEdtPass1.length());
            }
        });
        idCheckPass2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中，显示密码
                    idEdtPass2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    idEdtPass2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                idEdtPass2.setSelection(idEdtPass2.length());
            }
        });
    }
    private void isRight() {
        if (StringUtils.isEmpty(idEdtPass1.getText().toString()) || StringUtils.isEmpty(idEdtPass2.getText().toString())) {
            showToast("密码不能为空");
            return;
        }
        if (!idEdtPass1.getText().toString().equals(idEdtPass2.getText().toString())) {
            showToast("两次密码不一致");
            return;
        }
        initHttp();
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.FORGET_PASSWORD_STP2)
                .addParams("forget_password_token", forget_password_token)
                .addParams("NewsPassword", idEdtPass1.getText().toString())
                .addParams("cNewsPassword", idEdtPass2.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            showToast("修改成功");
                            intent2Activity(LoginActivity.class);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }


}
